<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="4.25rem">
      <el-form-item label="测评标题:" prop="scaleTitle" label-width="5rem">
        <el-input v-model="queryParams.scaleTitle" clearable @keyup.enter.native="handleQuery"
          style="width: 11.25rem;" />
      </el-form-item>
      <el-form-item label="状态:" prop="status" label-width="5rem">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 11.25rem;">
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="量表分类:" prop="scaleType" label-width="6rem">
        <el-select v-model="queryParams.scaleType" placeholder="请选择" clearable style="width: 11.25rem;">
          <el-option v-for="dict in dict.type.sys_scale_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="预警分数" prop="earlyWarningScore">
        <el-input v-model="queryParams.earlyWarningScore" placeholder="请输入预警分数" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:scale:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:scale:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:scale:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:scale:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scaleList" @selection-change="handleSelectionChange" tooltip-effect="dark myTooltips">
      <el-table-column type="selection" width="50" align="left" />
      <el-table-column label="测评量表id" align="left" prop="scaleId" min-width="90" />
      <el-table-column label="测评量表名称" align="left" min-width="130" :show-overflow-tooltip=true>
        <template slot-scope="scope">
          <!-- <el-tooltip v-if="isTextOverflow(scope.row.scaleTitle)" class="item" effect="dark"
            :content="scope.row.scaleTitle" placement="top-start">
            <div class="truncate-text"><a href="#">{{ scope.row.scaleTitle }}</a></div>
          </el-tooltip>
          <div v-else class="truncate-text">{{ scope.row.scaleTitle }}</div> -->
          {{ scope.row.scaleTitle }}
        </template>
      </el-table-column>
      <el-table-column label="测评量表显示名称" align="left" min-width="130" :show-overflow-tooltip=true>
        <template slot-scope="scope">
          {{ scope.row.scaleTitleShow }}
        </template>
      </el-table-column>
      <el-table-column label="测评量表详情" align="left" min-width="130" :show-overflow-tooltip=true>
        <template slot-scope="scope">
          <!-- <el-tooltip v-if="isTextOverflow(scope.row.scaleDetails)" class="item" effect="dark"
            :content="scope.row.scaleDetails" placement="top-start">
            <div class="truncate-text"><a href="#">{{ scope.row.scaleDetails }}</a></div>
          </el-tooltip> -->
          <!-- <div v-else class="truncate-text">
            {{ scope.row.scaleDetails }}</div> -->
            {{ scope.row.scaleDetails }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="发布者id" align="left" prop="userId" /> -->
      
      <el-table-column label="标准分数" align="left" prop="earlyWarningScore" />
      <el-table-column label="计分公式id" align="left" >
        <template slot-scope="scope">
        {{ scope.row.formulaId }}
        </template>
      </el-table-column>
      <el-table-column label="计分公式" align="left" placeholder="请选择" prop="formulaName">
        <template slot-scope="scope" >
        {{ scope.row.scaleFormula.formulaName }}
        </template>
      </el-table-column>
      <el-table-column label="量表类别" align="left" prop="scaleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_scale_type" :value="scope.row.scaleType" />
        </template>
      </el-table-column>
      <el-table-column label="测评开始时间" align="left" prop="startTime" min-width="100">
        <template slot-scope="scope">
          <!-- <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span> -->
          <span>{{ scope.row.startTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="测评时间" align="left" prop="scaleTime" />
      <el-table-column label="测评结束时间" align="left" prop="endTime" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="230">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:scale:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="addQuestion(scope.row)">添加问题</el-button>
          <el-button size="mini" type="text" icon="el-icon-list" @click="gotoQuestion(scope.row)">问题列表</el-button>
          <el-button size="mini" type="text" icon="el-icon-list" @click="gotoFactor(scope.row)">因子列表</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:scale:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    
    <el-pagination
      v-show="total>0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :total="total">
    </el-pagination>


    <!-- 添加或修改心理测评量表管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="45rem" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="9rem">
        <span>心理测评量表管理信息</span>
        <el-divider></el-divider>
        <el-form-item label="测评量表名称:" prop="scaleTitle">
          <el-input v-model="form.scaleTitle"  style="width: 20rem;"/>
        </el-form-item>
        <el-form-item label="测评量表显示名称:" prop="scaleTitleShow">
          <el-input v-model="form.scaleTitleShow"  style="width: 20rem;"/>
        </el-form-item>
        <el-form-item label="测评量表详情:" prop="scaleDetails">
          <el-input v-model="form.scaleDetails" :rows="4" type="textarea"  style="width: 20rem;"/>
        </el-form-item>
        <!-- <el-form-item label="发布者用户id:" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入发布者用户id" />
        </el-form-item> -->
        <el-form-item label="状态:" prop="status">
          <el-radio-group v-model="form.status" style="width: 20rem;">
            <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">
              {{ dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
         <!-- <el-input v-model="form.scaleFormula.formulaName" placeholder="请输入测评量表标题" /> -->
          <!-- 得到单个的，应该得到所有 -->
          <!-- <el-select v-model="form.scaleFormula.formulaName" >
            <el-option v-for="(item1,idx1) in scaleList" 
            :label="item1.scaleFormula.formulaName" 
            :value="item1.scaleFormula.formulaid"
            :key="idx1"
            ></el-option>
          </el-select> -->
        <el-form-item label="量表的总分计分公式:" prop="formulaName">
          <el-select v-model="form.scaleFormula.formulaName" @change="handleFormulaId" style="width: 20rem;">
            <el-option v-for="(item) in scaleFormulaList" 
            :label="item.formulaName" 
            :value="item.formulaName"
            :key="item.formulaId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标准分数:" prop="earlyWarningScore">
          <el-input v-model="form.earlyWarningScore"  style="width: 20rem;" type="number"/>
        </el-form-item>
        <el-form-item label="量表类别:" prop="scaleType" style="width: 100%;">
          <el-select v-model="form.scaleType" style="width: 20rem;">
            <el-option v-for="dict in dict.type.sys_scale_type" 
            :key="dict.value" 
            :label="dict.label"
            :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="测评开始时间:" prop="startTime">
          <!-- <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
            placeholder="yyyy-MM-dd" style="width: 20rem;">
          </el-date-picker> -->
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择日期时间"
            style="width: 20rem;">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="测评时间:" prop="scaleTime">
          <el-input v-model="form.scaleTime" style="width: 20rem;"/>
        </el-form-item> -->
        <el-form-item label="测评结束时间:" prop="endTime">
          <!-- <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="yyyy-MM-dd" style="width: 20rem;">
          </el-date-picker> -->
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择日期时间"
            style="width: 20rem;">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="更新时间:" prop="updateTiem">
          <el-date-picker clearable v-model="form.updateTiem" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-divider content-position="center">问题树信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddScaleQuestion">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteScaleQuestion">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="scaleQuestionList" :row-class-name="rowScaleQuestionIndex"
          @selection-change="handleScaleQuestionSelectionChange" ref="scaleQuestion">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="问题序号" prop="questionOrder" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.questionOrder" placeholder="请输入问题序号" />
            </template>
          </el-table-column>
          <el-table-column label="测评量表标题" prop="scaleTitle" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.scaleTitle" placeholder="请输入测评量表标题" />
            </template>
          </el-table-column>
          <el-table-column label="问题内容" prop="questionContent" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.questionContent" placeholder="请输入问题内容" />
            </template>
          </el-table-column>
          <el-table-column label="因子名称" prop="factorName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.factorName" placeholder="请输入因子名称" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加问题 -->
    <el-dialog :title="title" :visible.sync="openAddQuestion" width="45.25rem" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules" label-width="9rem">
        <el-form-item label="所属测评量表:" prop="scaleTitle" @change="handleScaleId">
          <el-select v-model="form1.scaleTitle" style="width: 17rem;">
            <el-option v-for="(item1) in scaleTitleList" 
            :label="item1.scaleTitle" 
            :value="item1.scaleTitle"
            :key="item1.scaleTitle"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 不要，后台增	 -->
        <!-- <el-form-item label="问题序号:" prop="questionOrder">
          <el-input v-model="form.questionOrder" placeholder="请输入预警分数" />
        </el-form-item> -->

        <el-form-item label="问题内容:" prop="questionContent">
          <el-input v-model="form1.questionContent"  style="width: 17rem;"/>
        </el-form-item>
      
        <el-form-item label="所属因子维度:" prop="factorName">
          <!-- <el-input v-model="form.factorName" /> -->
          <el-select v-model="form1.factorName" @change="handleFactorId" style="width: 17rem;">
            <el-option v-for="(item1) in factorNameList" 
            :label="item1.factorName" 
            :value="item1.factorName"
            :key="item1.factorName"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-divider content-position="center">问题选项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddScaleQuestion">增加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteScaleQuestion">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="scaleAnswerList" :row-class-name="rowScaleQuestionIndex"
          @selection-change="handleScaleQuestionSelectionChange" ref="scaleQuestion" tooltip-effect="dark myTooltips">
          <el-table-column type="selection"  align="center"  />
          <el-table-column label="序号" align="center" prop="index"/>
          <!-- <el-table-column label="问题序号" prop="questionOrder" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.questionOrder" placeholder="请输入问题序号" />
            </template>
          </el-table-column> -->
          <!-- <el-table-column label="测评量表标题" prop="scaleTitle" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.scaleTitle" placeholder="请输入测评量表标题" />
            </template>
          </el-table-column> -->
          
          <el-table-column label="选项" prop="answerOption" align="center">
            <!-- <el-input v-model="this.form1.answerOption"/> -->
            <template slot-scope="scope">
              <el-input v-model="scope.row.answerOption" />
            </template>
          </el-table-column>
          <el-table-column label="得分" prop="score" align="center">
            <!-- <el-input v-model="this.score" /> -->
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.score" @change="handleChange" label="描述文字" type="number"></el-input-number>
              <!-- <el-input v-model="scope.row.score" /> -->
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScale, getScale, delScale, addScale, updateScale } from "@/api/mentalTest/scaleManage";
import { listFormula,getFormula } from "@/api/mentalTest/formula"
import { addQuestionTree } from '@/api/mentalTest/questionInfo'
import { listFactor } from '@/api/system/factor'
export default {
  name: "ScaleManage",
  dicts: ['sys_normal_disable', 'sys_scale_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedScaleQuestion: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 心理测评量表管理表格数据
      scaleList: [],
      // 问题树信息表格数据
      scaleAnswerList: [],
      //测评量表公式表数据
      scaleFormulaList: [],
      //所属测评量表标题数据
      scaleTitleList: [],
      //因子维度数据
      factorNameList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      
      open: false,
      // 添加问题
      openAddQuestion: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scaleTitle: '',
        scaleTitleShow: '',
        userId: undefined,
        status: '',
        formulaId: undefined,
        scaleFormula:{
          formulaId: undefined,
          formulaName:'',
        },
        scaleType: '',
        earlyWarningScore: null,
      },
      //查询测评量表总分计分公式的参数
      // queryFormulaName:{
      //   //量表所有的总分计分公式
      //   formulaId: null,
      //   formulaName:null,
      // },
      //查询所属测评量表的参数
      queryTitle:{
        scaleId:null,
        scaleTitle: null,
      },
      //查询因子维度表的参数
      queryFactorName:{
        factorId: null,
        factorName:null,
      },
      // 表单参数
      form: {
        scaleId: undefined,
        scaleTitle: '',
        scaleTitleShow:'',
        scaleDetails: '',
        userId: undefined,
        status: '',
        formulaId: undefined,
        scaleFormula:{
          formulaId: undefined,
          formulaName:'',
        },
        scaleType: '',
        earlyWarningScore: null,
        startTime: null,
        scaleTime: null,
        endTime: null,
        createTime: null,
        updateTiem: null
      },
      //添加测评量表问题信息为获取所有测评量表
      form1:{
        // 新增到question表中
        //问题内容不用获取，是question中的question_content字段
        questionContent: '',
        scaleId: undefined,
        scaleTitle: '',
        scaleTitleShow:'',
        //要从factor中获取所有的因子维度
        factorId: undefined,
        factorName:'',
        scaleAnswerList:[]
      },
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getFormulaName();
    this.getList();
  },
  methods: {
    /** 查询心理测评量表管理列表 */
    getList() {
      this.loading = true;
      listScale(this.queryParams).then(response => {
        this.scaleList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log("所有心理测评量表管理列表",response.rows);
      });
    },
    //查询测评量表公式表
    getFormulaName() {
      listFormula(this.queryParams).then(response => {
        this.scaleFormulaList = response.rows
        console.log("获取所有测评量表公式",this.scaleFormulaList);
      })
    },
    //从questionInfo得到所有所属测评量表的标题
    getTitle() {
      listScale(this.queryTitle).then(response =>{
        this.scaleTitleList = response.rows
        console.log("所有所属测评量表",this.scaleTitleList);
      })
    },
    //要从factor中获取所有的因子维度
    getFactorName() {
      listFactor(this.queryFactorName).then(response =>{
        this.factorNameList = response.rows
        console.log("所有的因子维度",this.factorNameList);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openAddQuestion = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scaleId: undefined,
        scaleTitle: '',
        scaleTitleShow:'',
        scaleDetails: '',
        userId: undefined,
        status: null,
        formulaId: undefined,
        scaleFormula:{
          formulaName: '',
        },
        scaleType: '',
        earlyWarningScore: null,
        startTime: null,
        scaleTime: null,
        endTime: null,
        createTime: null,
        updateTiem: null
      };
      this.form1={
        questionContent: '',
        scaleId: undefined,
        scaleTitle: '',
        scaleTitleShow:'',
        factorId: undefined,
        factorName:'',
        scaleAnswerList:[]
      }
      this.scaleQuestionList = [];
      this.scaleAnswerList = [];
      this.resetForm("form");
      this.resetForm('form1')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 分页器页码改变事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
        // this.queryParams.pageNum = val
        console.log(`当前页: ${this.queryParams.pageNum}`);
        listScale({
          pageNum: val,
          pageSize: 10,
          scaleTitle: '',
          scaleTitleShow:'',
          scaleDetails: '',
          userId: undefined,
          status: '',
          formulaId: undefined,
          scaleFormula:{
            formulaId: undefined,
            formulaName:'',
          },
          scaleType: '',
          earlyWarningScore: null,
        }).then(response => {
          this.scaleList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.log('res',this.scaleList)
        });
      },
    //添加问题中的得分控件
    handleChange(value) {
        console.log(value);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scaleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加心理测评量表管理";
      this.form.status = "0";
      this.form.scaleFormula.formulaName = ""
      //******量表类别显示下拉框第一个没弄好
      // this.form.scaleType = dicts.scaleType[0]
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scaleId = row.scaleId || this.ids
      getScale(scaleId).then(response => {
        this.form = response.data;
        this.scaleQuestionList = response.data.scaleQuestionList;
        this.open = true;
        this.title = "修改心理测评量表管理";
        getFormula(this.form.formulaId).then(response1 => {
          this.form.scaleFormula.formulaName = response1.data.formulaName
          // console.log("111",response1.data);
        })
      });
    },
    // 添加问题
    addQuestion(row) {
      this.reset();
      const scaleId = row.scaleId
      getScale(scaleId).then(response => {
        this.form = response.data;
        console.log("123456",this.form.scaleTitle);
        this.form1.scaleId = scaleId;
        this.form1.scaleTitle = this.form.scaleTitle;
        this.form1.scaleTitleShow = this.form.scaleTitleShow;
        this.form1.factorName = ""
        this.getTitle();
        this.getFactorName();
        this.openAddQuestion = true;
        this.title = "添加问题";
      });
    },

    /** 提交按钮 */
    submitForm() {
      let dateObject = new Date(this.form.startTime);
      // 获取年、月、日、小时、分钟和秒
      let year = dateObject.getFullYear();
      let month = (dateObject.getMonth() + 1).toString().padStart(2, '0'); // 月份从 0 开始，所以要加 1
      let day = dateObject.getDate().toString().padStart(2, '0');
      let hours = dateObject.getHours().toString().padStart(2, '0');
      let minutes = dateObject.getMinutes().toString().padStart(2, '0');
      let seconds = dateObject.getSeconds().toString().padStart(2, '0');
      // 格式化为所需的字符串
      this.form.startTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      dateObject = new Date(this.form.endTime);
      // 获取年、月、日、小时、分钟和秒
      year = dateObject.getFullYear();
      month = (dateObject.getMonth() + 1).toString().padStart(2, '0'); // 月份从 0 开始，所以要加 1
      day = dateObject.getDate().toString().padStart(2, '0');
      hours = dateObject.getHours().toString().padStart(2, '0');
      minutes = dateObject.getMinutes().toString().padStart(2, '0');
      seconds = dateObject.getSeconds().toString().padStart(2, '0');
      // 格式化为所需的字符串
      this.form.endTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      console.log("form信息",this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.scaleAnswerList = this.scaleAnswerList;
          if (this.form.scaleId != null) {
            updateScale(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(this.form.scaleFormula.formulaName == "请选择"){
              console.log("不能动");
            }else{
              // console.log("123",this.form.formulaId);
              // console.log("234",this.form.scaleFormula.formulaName);
              addScale(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    //为得到formulaId的回调函数
    handleFormulaId(selectedValue) {
      const selectedFormula = this.scaleFormulaList.find(item => item.formulaName === selectedValue);
      if (selectedFormula) {
        const formulaId = selectedFormula.formulaId;
        this.form.formulaId=formulaId
        // console.log('Selected formulaId:', formulaId);
      }
    },
    //为得到scaleId的回调函数
    handleScaleId(selectedValue) {
      const selectedScale = this.scaleTitleList.find(item => item.scaleTitle === selectedValue);
      if (selectedScale) {
        const scaleId = selectedScale.scaleId;
        this.form.scaleId=scaleId
        this.form1.scaleId = scaleId;
        console.log("scaleId",scaleId);
      }
    },
    //为得到factorId的回调函数
    handleFactorId(selectedValue) {
      const selectedFactor = this.factorNameList.find(item => item.factorName === selectedValue);
      if (selectedFactor) {
        const factorId = selectedFactor.factorId;
        this.form.factorId=factorId
        this.form1.factorId = factorId;
        console.log("factorId",factorId);
      }
    },
    //提交按钮
    submitForm1() {
      console.log("form1信息",this.form1);
      this.$refs["form1"].validate(valid => {
        if (valid) {
          this.form1.scaleAnswerList = this.scaleAnswerList;
          console.log("555",this.form1);
          addQuestionTree(this.form1).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.openAddQuestion = false;
            this.getList();
          });
        }else{
          this.$message.error("请填写必要的字段");
        }
      });
    },

    // 问题列表函数
    gotoQuestion(row) {
      this.$router.push({
        path:'/mentalTest/scaleSet/questionInfo',
        query:{
          scaleTitle:row.scaleTitle
        }
      })
    },
    //测评因子列表
    gotoFactor(row){
      this.$router.push({
        path:'/mentalTest/scaleSet/factorInfo',
        query:{
          scaleTitle:row.scaleTitle
          // scaleTitle:''
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const scaleIds = row.scaleId || this.ids;
      this.$modal.confirm('是否确认删除心理测评量表管理编号为"' + scaleIds + '"的数据项？').then(function () {
        return delScale(scaleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 问题树信息序号 */
    rowScaleQuestionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 问题树信息添加按钮操作 */
    handleAddScaleQuestion() {
      let obj = {};
      obj.answerOption = "";
      obj.score = "";
      this.scaleAnswerList.push(obj);
    },
    /** 问题树信息删除按钮操作 */
    handleDeleteScaleQuestion() {
      if (this.checkedScaleQuestion.length == 0) {
        this.$modal.msgError("请先选择要删除的问题树信息数据");
      } else {
        const scaleAnswerList = this.scaleAnswerList;
        const checkedScaleQuestion = this.checkedScaleQuestion;
        this.scaleAnswerList = scaleAnswerList.filter(function (item) {
          return checkedScaleQuestion.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleScaleQuestionSelectionChange(selection) {
      this.checkedScaleQuestion = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/scale/export', {
        ...this.queryParams
      }, `scale_${new Date().getTime()}.xlsx`)
    },

    /* 判断文本是否溢出tooltip显示*/
    isTextOverflow(text) {
      // Create a temporary element to measure text overflow
      const el = document.createElement('div');
      el.style.position = 'absolute';
      el.style.visibility = 'hidden';
      el.style.whiteSpace = 'nowrap';
      el.style.width = 'auto';
      el.innerHTML = text;
      document.body.appendChild(el);
      // Check if the width of the element exceeds its parent's width
      const isOverflow = el.offsetWidth > el.parentElement.offsetWidth;
      document.body.removeChild(el);
      return isOverflow;
    },
  }
};
</script>
<style lang="scss" scoped>
// .truncate-text {
//   white-space: nowrap;
//   overflow: hidden;
//   text-overflow: ellipsis;
// }
</style>
<style>
.myTooltips{
  background-color: rgb(108, 195, 230)!important;
   color: #fff;
   border: 1px solid #333;
   max-width: 30%;
   text-indent:2em;
}
</style>